using Microsoft.AspNetCore.Mvc;
using SheLife.Application.Services;
using SheLife.Domain.Entities;

namespace SheLife.AdminApi.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class HabitRecordsController : ControllerBase
    {
        private readonly IHabitRecordService _habitRecordService;

        public HabitRecordsController(IHabitRecordService habitRecordService)
        {
            _habitRecordService = habitRecordService;
        }

        [HttpGet("{id}")]
        public async Task<IActionResult> GetHabitRecordById(int id)
        {
            var habitRecord = await _habitRecordService.GetHabitRecordByIdAsync(id);
            if (habitRecord == null)
            {
                return NotFound();
            }
            return Ok(habitRecord);
        }

        [HttpGet("user/{userId}")]
        public async Task<IActionResult> GetHabitRecordsByUserId(int userId)
        {
            var habitRecords = await _habitRecordService.GetHabitRecordsByUserIdAsync(userId);
            return Ok(habitRecords);
        }

        [HttpGet("habit/{habitId}")]
        public async Task<IActionResult> GetHabitRecordsByHabitId(int habitId)
        {
            var habitRecords = await _habitRecordService.GetHabitRecordsByHabitIdAsync(habitId);
            return Ok(habitRecords);
        }

        [HttpGet("user/{userId}/daterange")]
        public async Task<IActionResult> GetHabitRecordsByUserIdAndDateRange(
            int userId, 
            [FromQuery] DateTime startDate, 
            [FromQuery] DateTime endDate)
        {
            var habitRecords = await _habitRecordService.GetHabitRecordsByUserIdAndDateRangeAsync(userId, startDate, endDate);
            return Ok(habitRecords);
        }

        [HttpGet("user/{userId}/habit/{habitId}/date")]
        public async Task<IActionResult> GetHabitRecordsByUserIdAndHabitIdAndDate(
            int userId, 
            int habitId, 
            [FromQuery] DateTime date)
        {
            var habitRecords = await _habitRecordService.GetHabitRecordsByUserIdAndHabitIdAndDateAsync(userId, habitId, date);
            return Ok(habitRecords);
        }

        [HttpPost]
        public async Task<IActionResult> CreateHabitRecord([FromBody] HabitRecord habitRecord)
        {
            var createdHabitRecord = await _habitRecordService.CreateHabitRecordAsync(habitRecord);
            return CreatedAtAction(nameof(GetHabitRecordById), new { id = createdHabitRecord.Id }, createdHabitRecord);
        }

        [HttpPut("{id}")]
        public async Task<IActionResult> UpdateHabitRecord(int id, [FromBody] HabitRecord habitRecord)
        {
            if (id != habitRecord.Id)
            {
                return BadRequest();
            }

            var updatedHabitRecord = await _habitRecordService.UpdateHabitRecordAsync(habitRecord);
            if (updatedHabitRecord == null)
            {
                return NotFound();
            }
            return Ok(updatedHabitRecord);
        }

        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteHabitRecord(int id)
        {
            var result = await _habitRecordService.DeleteHabitRecordAsync(id);
            if (!result)
            {
                return NotFound();
            }
            return NoContent();
        }
    }
}